<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */
function addLine($text, $data)
{
    echo "\n" . '<tr><td style="text-align: left;">' . $text .
        '</td><td style="text-align: right;">' . $data . '</td></tr>';
}

if ($q[1] == 'single' && !empty($q[2])) {

    if (is_numeric($q[2])) {
        $where = "id = '" . intval($q[2]) . "'";
    } else {
        $where = "username = '" . Registry::get('db')->real_escape_string($q[2]) . "'";
    }

    $query = Registry::get('db')->query("SELECT * FROM {prefix}users WHERE " . $where . " LIMIT 1");

    if ((int)$query->num_rows != 0) {
        
        /*if( User::isLogged() )
        {
            //TODO:Friends support
            echo "<p class=\"com_user_friend_panel\">\n
            <input type=\"buttom\" value=\"Přidat jako přítele\" onclick=\"\" /> <input type=\"buttom\" value=\"Přidat jako nepřítele\" onclick=\"\" />
            \n</p>\n";
        }*/

        echo '<table cellspacing="2" >';
        $data = $query->fetch_array();

        $comment_count = Registry::get('db')->query("SELECT COUNT(id) FROM {prefix}comments WHERE author_id = '" . intval($data['id']) . "'");
        $comment_count = $comment_count->fetch_array();


        $articles_count = Registry::get('db')->query("SELECT COUNT(id) FROM {prefix}articles WHERE author_id = '" . intval($data['id']) . "'");
        $articles_count = $articles_count->fetch_array();

        echo '<tr><th><h2>'.Lang::getFront('com_user', 'personal').'</h2></th></tr>';
        addLine(Lang::getFront('com_user', 'id'), $data['id']);
        addLine(Lang::getFront('com_user', 'nickname'), $data['display_name']);
        addLine(Lang::getFront('com_user', 'fullname'), $data['real_name']);
        addLine(Lang::getFront('com_user', 'avatar'), '<img src="{url}components/com_user/avatars/' . $data['avatar'] . '" alt="avatar" />');
        addLine(Lang::getFront('com_user', 'registered_from'), $data['created']);
        addLine(Lang::getFront('com_user', 'rank'), '<img src="{url}libs/ranks/' . $GLOBALS['setting']['ranks'] .
            '/' . getImageOfRank($data['rank']) . '" />');
        if ($data['display_email'] == 1) {
            addLine(Lang::getFront('com_user', 'email'), $data['email']);
        }
        addLine(Lang::getFront('com_user', 'birthday'), $data['birthday']);
        addLine(Lang::getFront('com_user', 'gender'), $data['gender']);
        addLine(Lang::getFront('com_user', 'website'), '<a href="' . $data['website'] . '" target="_blank">' . $data['website'] .
            '</a>');

        echo '<tr><th><h2>'.Lang::getFront('com_user', 'stats').'</h2></th></tr>';
        addLine(Lang::getFront('com_user', 'stats_comments'), $comment_count[0]);
        addLine(Lang::getFront('com_user', 'stats_articles'), $articles_count[0]);

        if ($data['display_contact'] == 1) {

            if (isset($_POST['submit']) && !empty($_POST['text'])) {
                if (Tools::mail($data['email'], strip_tags($_POST['subject']), strip_tags($_POST['text']))) {
                    echo '<h3>'.Lang::getFront('com_user', 'contactform_true').'</h3>';
                } else {
                    echo '<h3>'.Lang::getFront('com_user', 'contactform_false').'</h3>';
                }
            }


            $formular = new Form("", 'post');
            echo '<tr><th><h2>'.Lang::getFront('com_user', 'contactform_headline').'</h2></th></tr>';
            $email = array('type' => 'text', 'name' => 'sender', 'label' => Lang::getFront('com_user', 'contactform_email'));
            $formular->addElement('input', $email);
            $input = array('type' => 'text', 'name' => 'subject', 'label' => Lang::getFront('com_user', 'contactform_subject'));
            $formular->addElement('input', $input);
            $textarea = array('type' => 'text', 'name' => 'text', 'cols' => '35', 'rows' =>
                '7', 'label' => Lang::getFront('com_user', 'contactform_text'));
            $formular->addElement('textarea', $textarea);
            $submit = array('type' => 'submit', 'name' => 'submit', 'value' => Lang::getFrontGlobal('submit'));
            $formular->addElement('input', $submit);
            $formular->render();
        }

        echo '</table>';

        $query = $db->query("SELECT * FROM {prefix}fields_data WHERE uid = '" . intval($q[2]) .
            "'");
        $fields_data = $query->fetch_array();
        unset($query);
        $cats = $db->query("SELECT id, title FROM {prefix}fields_cats");
        while ($d = $cats->fetch_array()) {
            echo "\n<table>";
            echo "\n<tr><th><h2>" . $d['title'] . "</h2></th></tr>";

            $fields = $db->query("SELECT * FROM {prefix}fields WHERE catid = '" . $d['id'] .
                "'");
            while ($field = $fields->fetch_array()) {
                addLine($field['desc'], $fields_data[$field['sql_name']]);
            }

            echo "\n</table>";
        }
        
        Text::br();
        
        Text::h2( Lang::getFront('com_user', 'friends') );
        
        $query = $db->query("SELECT * FROM {prefix}user_friends WHERE uid = '".intval($q[2])."'");
        
        echo "\n<table class=\"com_user_friends\">";
        
        while($data = $query->fetch_array())
        {
            echo "<tr>\n
            <td><img src=\"{url}components/com_user/avatars/".User::getAvatar(intval($data['fid']))."\" alt=\"Avatar\" /></td>\n
            <td><a href=\"{url}user/single/".User::getUsername(intval($data['fid']))."\">".User::getDisplayName(intval($data['fid']))."</a></td>\n
            <td><a href=\"{url}user/messages/send/?to=".intval($data['fid'])."\"><img src=\"{url}libs/icons/pencil.png\" alt=\"Message\" /></a></td>\n
            <td><img src=\"{url}libs/icons/".$data['status'].".png\" alt=\"Status\" /></td>\n
            </tr>\n
            ";
        }
        
        echo "\n</table>";
        
        Text::br();
        
        Text::h2( Lang::getFront('com_user', 'comments') );
        
        define('COM_COMMENT_BOOT', true);
        define('COM_COMMENT_COMPONENT', 'com_user');
        define('COM_COMMENT_CID', intval($data['id']));
        
        require ( APPROOT . '/components/com_comment/com_comment.php');

    } else {
        echo "<p class=\"msg error\">".Lang::getFront('com_user', 'notfound')."</h2>";
    }
}

?>